package com.minzheng.blog.controller;

import com.minzheng.blog.dao.PhotoDao;
import com.minzheng.blog.dto.PhotoBackDTO;
import com.minzheng.blog.dto.PhotoDTO;
import com.minzheng.blog.service.ArticleService;
import com.minzheng.blog.service.BlogInfoService;
import com.minzheng.blog.service.impl.TestImpl;
import com.minzheng.blog.util.MarkdownUtil;
import com.minzheng.blog.vo.MarkdownContent;
import com.minzheng.blog.vo.WebsiteConfigVO;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.util.data.MutableDataSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import com.vladsch.flexmark.parser.Parser;

import com.vladsch.flexmark.util.ast.Node;

import java.util.List;

/**
 * @author wangdiyi
 * @date 2023/4/8 21:11:00
 */
@Controller
public class ViewController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private PhotoDao photoDao;
    @Autowired
    private BlogInfoService blogInfoService;


    @RequestMapping("/index1")
    public String index1(){ return "index1"; }

    @RequestMapping("/")
    public String index2(Model model){
        List<PhotoBackDTO> photoDaoListone=photoDao.listFrontOnePhoto();
        List<PhotoBackDTO> photoDaoList=photoDao.listFrontSysPhoto();
        List<PhotoBackDTO> photoBackDTOList=photoDao.listFrontFirstPhoto();
        List<PhotoBackDTO> photoBackDTOList1=photoDao.listFrontSecondPhoto();
        List<PhotoBackDTO> photoBackDTOList2=photoDao.listFrontThree1Photo();
        List<PhotoBackDTO> photoBackDTOList3=photoDao.listFrontThree2Photo();
        List<PhotoBackDTO> photoBackDTOList4=photoDao.listFrontFivePhoto();
        //获取首页01部分配图
        PhotoBackDTO first_one=photoBackDTOList.get(0);
        PhotoBackDTO first_two=photoBackDTOList.get(1);
        //获取首页02部分获奖图
        PhotoBackDTO sencond_one=photoBackDTOList1.get(0);
        PhotoBackDTO sencond_two=photoBackDTOList1.get(1);
        PhotoBackDTO sencond_three=photoBackDTOList1.get(2);
        //获取首页03部分竞赛组配图
        PhotoBackDTO three_one_one=photoBackDTOList2.get(0);//打底
//        PhotoBackDTO three_one_two=photoBackDTOList2.get(1);
        //获取首页03部分项目组配图
        PhotoBackDTO three_two=photoBackDTOList3.get(0);
        //获取首页05部分师长寄语配图
        PhotoBackDTO five=photoBackDTOList4.get(0);
        model.addAttribute("OnePhotoList",photoDaoListone);

        model.addAttribute("SysPhotos",photoDaoList);
        model.addAttribute("FirstPhotos_one",first_one);
        model.addAttribute("FirstPhotos_two",first_two);

        model.addAttribute("SecondPhotos_one",sencond_one);
        model.addAttribute("SecondPhotos_two",sencond_two);
        model.addAttribute("SecondPhotos_three",sencond_three);

        model.addAttribute("ThreePhotos_one_one",three_one_one);
//        model.addAttribute("ThreePhotos_one_two",three_one_two);
        model.addAttribute("ThreePhotos_two",three_two);

        model.addAttribute("FivePhotos",five);

        WebsiteConfigVO websiteConfigVO=new WebsiteConfigVO();
        websiteConfigVO=blogInfoService.getWebsiteConfig();
        model.addAttribute("websiteConfigVO", websiteConfigVO);
        System.out.println(websiteConfigVO.getWebsiteAvatar());
        System.out.println(websiteConfigVO.getWebsiteRecordNo());
        System.out.println(websiteConfigVO.getWebsiteName());
        return "index-2"; }

    @RequestMapping("/index3")
    public String index3(){ return "index-3"; }

    @RequestMapping("/gallery")
    public String gallery(){ return "gallery"; }

    @RequestMapping("/faq")
    public String faq(){ return "faq"; }

    @RequestMapping("/contact")
    public String contact(Model model){
        String about=new String();
        about= MarkdownUtil.markdownExtToHtml(blogInfoService.getAbout());
        System.out.println(about);
        model.addAttribute("about",about);
        return "contact";
    }

    @RequestMapping("/blog-standard")
    public String blog_standard(){ return "blog-standard"; }

    @RequestMapping("/blog-details")
    public String blog_details(){ return "blog-details"; }

    @RequestMapping("/accommodation-standard")
    public String accommodation_standard(){ return "newslist"; }

    @RequestMapping("/accommodation-slider")
    public String accommodation_slider(){ return "accommodation-slider"; }

    @RequestMapping("/accommodation-grid")
    public String accommodation_grid(){ return "peopleslist"; }

    @RequestMapping("/accommodation-details")
    public String accommodation_details(){ return "accommodation-details"; }

    @RequestMapping("/newstest")
    public String newstest(){ return "newstest"; }

    @RequestMapping("/mdtest")
    public String mdtest(){ return "mdtest"; }

    @RequestMapping("/front/diagram")
    public String diagram(){ return "diagram"; }



    @Resource
    private TestImpl markdownContentService;

    @GetMapping("/markdown/{id}")
    public String showMarkdownContent(@PathVariable Integer id, Model model) {
        // 获取 Markdown 内容
        MarkdownContent markdownContent = markdownContentService.getMarkdownContentById(id);

        // 使用 Flexmark 将 Markdown 转换为 HTML
        MutableDataSet options = new MutableDataSet();
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();
        Node document = parser.parse(markdownContent.getContent());
        String htmlContent = renderer.render(document);

        // 将转换后的 HTML 添加到模型中
        model.addAttribute("htmlContent", htmlContent);
        return "markdown";
    }

    @GetMapping("/getWebConfig")
    public String getWebConfig(@PathVariable Integer id, Model model) {
        // 获取 Markdown 内容
        MarkdownContent markdownContent = markdownContentService.getMarkdownContentById(id);

        // 使用 Flexmark 将 Markdown 转换为 HTML
        MutableDataSet options = new MutableDataSet();
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();
        Node document = parser.parse(markdownContent.getContent());
        String htmlContent = renderer.render(document);

        // 将转换后的 HTML 添加到模型中
        model.addAttribute("htmlContent", htmlContent);
        return "markdown";
    }

}
